﻿using JsGame;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JsEditor
{
    public partial class ObjChooseForm : Form
    {

        public ObjChooseForm()
        {
            InitializeComponent();
        }

        Button[] bOthers;
        int other_value;

        public int BeginChoose(Object[] list,int idx,JsListItem[] other_button=null)
        {
            tbMask.Clear();
            lbObj.BeginUpdate();
            lbObj.Items.Clear();
            lbObj.Items.AddRange(list);
            if (idx < lbObj.Items.Count)
                lbObj.SelectedIndex = idx;
            lbObj.EndUpdate();

            if (bOthers != null)
                foreach (var b in bOthers)
                    Controls.Remove(b);

            if (other_button != null)
            {
                bOthers = new Button[other_button.Length];
                var loc = bCancel.Location;
                var size = bCancel.Size;
                for (int i = 0; i < bOthers.Length; i++)
                {
                    bOthers[i] = new Button();
                    bOthers[i].Text = other_button[i].name;
                    bOthers[i].Tag = other_button[i].value;
                    loc.Y += size.Height + 16;
                    bOthers[i].Size = size;
                    bOthers[i].Location = loc;
                    bOthers[i].Click += new EventHandler(bOther_Click);
                }
                Controls.AddRange(bOthers);
            }



            var result=ShowDialog();
            while (Visible)
                Application.DoEvents();
            if (result == DialogResult.OK)
                return lbObj.SelectedIndex;
            else if (result == DialogResult.Retry)
                return other_value;
            return -1;
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void bOther_Click(object sender, EventArgs e)
        {
            other_value = int.Parse(((Button)(sender)).Tag.ToString());
            DialogResult = DialogResult.Retry;
            Close();
        }


        private void tbMask_TextChanged(object sender, EventArgs e)
        {
            lbObj.SelectedIndex = -1;
            lbObj.FindItem(tbMask.Text);
        }


        private void bNext_Click(object sender, EventArgs e)
        {
            lbObj.FindItem(tbMask.Text);
        }




    }
}
